//Tan Kok Hui
import java.util.Vector;
import org.apache.log4j.Logger;

public class Storage {
	static Logger logger = Logger.getLogger(Controller.class.getName());
	private Vector<Task> tasks;
	private FileIO io;

	public Storage(String fileName) {
		assert fileName != null;
		
		io = new FileIO(fileName);

		if (io.fileExists())
			tasks = io.readFromFile();
		else
			tasks = new Vector<Task>();
		
		String log = fileName + ": Storage is ready.";
		logger.info(log);
	}

	public void add(Task newTask, int position) {
		//System.out.println("newTask.getID() = " + newTask.getID());
		String log = "Adding " + newTask.getDescription() + " at position "
				+ position;
		logger.info(log);
		assert newTask != null;
		boolean validPosition = true;
		if (!((position >= -1)))
			validPosition = false;
		assert validPosition;
		if (searchByTaskID(newTask.getID()) == -1) {
			if (position == -1)
				tasks.add(newTask);
			else if (position <= tasks.size())
				tasks.add(position, newTask);
			else
				tasks.add(tasks.size(), newTask);

			log = newTask.getDescription() + " is added.";
			logger.info(log);
			return;
		}
	}

	public Vector<Task> retrieveData() {
		return tasks;
	}

	public int deleteTask(String taskID) {
		String log = "Deleting a Task with an ID of " + taskID;
		logger.info(log);
		assert taskID != null;
		
		int result = -1;
		try {
			result = searchByTaskID(taskID);
			if (result != -1) {
				log = tasks.get(result).getDescription() + " is deleted.";
				tasks.remove(result);
				logger.info(log);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		
		return result;
	}

	
	
	public int searchByTaskID(String taskID) {
		String log = "Searching for a Task with an ID of " + taskID;
		logger.info(log);
		assert taskID != null;
		int result = -1; // no task with such ID
		for (int i = 0; i < tasks.size(); i++)
			if (tasks.get(i).getID().equals(taskID)) {
				result = i;
				log = tasks.get(i).getDescription() + " is found.";
				logger.info(log);
			}
		return result;
	}

	public void updateFile() {
		io.writeToFile(tasks);
		logger.info("File is updated.");
		return;
	}

	public Task retrieveTask(String taskID) {
		String log = "Retrieving a Task with an ID of " + taskID;
		logger.info(log);
		assert taskID != null;
		
		int i = searchByTaskID(taskID);
		if (i == -1)
			return null;
		
		log = tasks.get(i).getDescription() + " is retrieved.";
		logger.info(log);
		return tasks.get(i);
	}

	public Vector<Task> search(String keyword) {
		String log = "Searching for a Task with keyword = " + keyword;
		logger.info(log);
		assert keyword != null;
		
		Vector<Task> searchResult = new Vector<Task>();
		for (int i = 0; i < tasks.size(); i++) {
			searchResult.add(tasks.get(i));
		}
		String[] word = keyword.split(" ");
		boolean contain;

		for (int i = 0; i < word.length; i++) {
			int n = word[i].length();

			for (int j = 0; j < searchResult.size(); j++) {
				contain = false;
				int m = searchResult.get(j).getDescription().length()
						+ searchResult.get(j).getDate().length()
						+ searchResult.get(j).getTime().length();

				for (int k = 0; k <= (m - n); k++) {
					String s = searchResult.get(j).getDescription()
							.concat(searchResult.get(j).getDate())
							.concat(searchResult.get(j).getTime())
							.substring(k, k + n);
					if (s.compareToIgnoreCase(word[i]) == 0) {
						contain = true;
						break;
					}
				}

				if (!contain) {
					searchResult.remove(j);
					j--;
				}

				if (searchResult.size() == 0)
					break;
			}
			
			if (searchResult.size() == 0)
				break;
		}

		return searchResult;
	}
}